package com.fsh.oss.service;

import com.fsh.oss.adapter.StorageAdapter;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FileService {

    @Resource
    private StorageAdapter storageAdapter;

    /**
     * 列出所有桶
     */
    public List<String> getAllBuckets() {
        return storageAdapter.getAllBuckets();
    }

    /**
     * 创建桶
     */
    public void createBucket(String bucketName){
        storageAdapter.createBucket(bucketName);
    }

    /**
     *  获取文件的  url
     */
    @RequestMapping("/getUrl")
    public String getUrl(String bucketName, String objectName) throws Exception {
        return storageAdapter.getUrl(bucketName, objectName);
    }

    /**
     * 上传文件
     */
    public String uploadFile(MultipartFile uploadFile, String bucket, String objectName){
        storageAdapter.uploadFile(uploadFile,bucket,objectName);
        objectName=objectName+"/"+uploadFile.getOriginalFilename();
        return storageAdapter.getUrl(bucket, objectName);
    }
}
